/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ep.persistence.controllers;

import ep.persistence.controllers.exceptions.IllegalOrphanException;
import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.T004Insurance;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ep.persistence.entities.T005Patient;
import java.util.ArrayList;
import java.util.Collection;
import ep.persistence.entities.T002Doctor;

/**
 *
 * @author Spyros
 */
public class T004InsuranceJpaController {

    public T004InsuranceJpaController() {
        emf = Persistence.createEntityManagerFactory("EprescPersistencePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(T004Insurance t004Insurance) {
//        if (t004Insurance.getT005PatientCollection() == null) {
//            t004Insurance.setT005PatientCollection(new ArrayList<T005Patient>());
//        }
        if (t004Insurance.getT002Doctors() == null) {
            t004Insurance.setT002Doctors(new ArrayList<T002Doctor>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<T005Patient> attachedT005PatientCollection = new ArrayList<T005Patient>();
//            for (T005Patient t005PatientCollectionT005PatientToAttach : t004Insurance.getT005PatientCollection()) {
//                t005PatientCollectionT005PatientToAttach = em.getReference(t005PatientCollectionT005PatientToAttach.getClass(), t005PatientCollectionT005PatientToAttach.getId());
//                attachedT005PatientCollection.add(t005PatientCollectionT005PatientToAttach);
//            }
//            t004Insurance.setT005PatientCollection(attachedT005PatientCollection);
            Collection<T002Doctor> attachedT002Doctors = new ArrayList<T002Doctor>();
            for (T002Doctor t002DoctorsT002DoctorToAttach : t004Insurance.getT002Doctors()) {
                t002DoctorsT002DoctorToAttach = em.getReference(t002DoctorsT002DoctorToAttach.getClass(), t002DoctorsT002DoctorToAttach.getId());
                attachedT002Doctors.add(t002DoctorsT002DoctorToAttach);
            }
            t004Insurance.setT002Doctors(attachedT002Doctors);
            em.persist(t004Insurance);
//            for (T005Patient t005PatientCollectionT005Patient : t004Insurance.getT005PatientCollection()) {
//                T004Insurance oldT004InsuranceOfT005PatientCollectionT005Patient = t005PatientCollectionT005Patient.getT004Insurance();
//                t005PatientCollectionT005Patient.setT004Insurance(t004Insurance);
//                t005PatientCollectionT005Patient = em.merge(t005PatientCollectionT005Patient);
//                if (oldT004InsuranceOfT005PatientCollectionT005Patient != null) {
//                    oldT004InsuranceOfT005PatientCollectionT005Patient.getT005PatientCollection().remove(t005PatientCollectionT005Patient);
//                    oldT004InsuranceOfT005PatientCollectionT005Patient = em.merge(oldT004InsuranceOfT005PatientCollectionT005Patient);
//                }
//            }
            for (T002Doctor t002DoctorsT002Doctor : t004Insurance.getT002Doctors()) {
                t002DoctorsT002Doctor.getT004Insurances().add(t004Insurance);
                t002DoctorsT002Doctor = em.merge(t002DoctorsT002Doctor);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(T004Insurance t004Insurance) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T004Insurance persistentT004Insurance = em.find(T004Insurance.class, t004Insurance.getId());
//            Collection<T005Patient> t005PatientCollectionOld = persistentT004Insurance.getT005PatientCollection();
//            Collection<T005Patient> t005PatientCollectionNew = t004Insurance.getT005PatientCollection();
            Collection<T002Doctor> t002DoctorsOld = persistentT004Insurance.getT002Doctors();
            Collection<T002Doctor> t002DoctorsNew = t004Insurance.getT002Doctors();
            List<String> illegalOrphanMessages = null;
//            for (T005Patient t005PatientCollectionOldT005Patient : t005PatientCollectionOld) {
//                if (!t005PatientCollectionNew.contains(t005PatientCollectionOldT005Patient)) {
//                    if (illegalOrphanMessages == null) {
//                        illegalOrphanMessages = new ArrayList<String>();
//                    }
//                    illegalOrphanMessages.add("You must retain T005Patient " + t005PatientCollectionOldT005Patient + " since its t004Insurance field is not nullable.");
//                }
//            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
//            Collection<T005Patient> attachedT005PatientCollectionNew = new ArrayList<T005Patient>();
//            for (T005Patient t005PatientCollectionNewT005PatientToAttach : t005PatientCollectionNew) {
//                t005PatientCollectionNewT005PatientToAttach = em.getReference(t005PatientCollectionNewT005PatientToAttach.getClass(), t005PatientCollectionNewT005PatientToAttach.getId());
//                attachedT005PatientCollectionNew.add(t005PatientCollectionNewT005PatientToAttach);
//            }
//            t005PatientCollectionNew = attachedT005PatientCollectionNew;
//            t004Insurance.setT005PatientCollection(t005PatientCollectionNew);
            Collection<T002Doctor> attachedT002DoctorsNew = new ArrayList<T002Doctor>();
            for (T002Doctor t002DoctorsNewT002DoctorToAttach : t002DoctorsNew) {
                t002DoctorsNewT002DoctorToAttach = em.getReference(t002DoctorsNewT002DoctorToAttach.getClass(), t002DoctorsNewT002DoctorToAttach.getId());
                attachedT002DoctorsNew.add(t002DoctorsNewT002DoctorToAttach);
            }
            t002DoctorsNew = attachedT002DoctorsNew;
            t004Insurance.setT002Doctors(t002DoctorsNew);
            t004Insurance = em.merge(t004Insurance);
//            for (T005Patient t005PatientCollectionNewT005Patient : t005PatientCollectionNew) {
//                if (!t005PatientCollectionOld.contains(t005PatientCollectionNewT005Patient)) {
//                    T004Insurance oldT004InsuranceOfT005PatientCollectionNewT005Patient = t005PatientCollectionNewT005Patient.getT004Insurance();
//                    t005PatientCollectionNewT005Patient.setT004Insurance(t004Insurance);
//                    t005PatientCollectionNewT005Patient = em.merge(t005PatientCollectionNewT005Patient);
//                    if (oldT004InsuranceOfT005PatientCollectionNewT005Patient != null && !oldT004InsuranceOfT005PatientCollectionNewT005Patient.equals(t004Insurance)) {
//                        oldT004InsuranceOfT005PatientCollectionNewT005Patient.getT005PatientCollection().remove(t005PatientCollectionNewT005Patient);
//                        oldT004InsuranceOfT005PatientCollectionNewT005Patient = em.merge(oldT004InsuranceOfT005PatientCollectionNewT005Patient);
//                    }
//                }
//            }
            for (T002Doctor t002DoctorsOldT002Doctor : t002DoctorsOld) {
                if (!t002DoctorsNew.contains(t002DoctorsOldT002Doctor)) {
                    t002DoctorsOldT002Doctor.getT004Insurances().remove(t004Insurance);
                    t002DoctorsOldT002Doctor = em.merge(t002DoctorsOldT002Doctor);
                }
            }
            for (T002Doctor t002DoctorsNewT002Doctor : t002DoctorsNew) {
                if (!t002DoctorsOld.contains(t002DoctorsNewT002Doctor)) {
                    t002DoctorsNewT002Doctor.getT004Insurances().add(t004Insurance);
                    t002DoctorsNewT002Doctor = em.merge(t002DoctorsNewT002Doctor);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = t004Insurance.getId();
                if (findT004Insurance(id) == null) {
                    throw new NonexistentEntityException("The t004Insurance with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T004Insurance t004Insurance;
            try {
                t004Insurance = em.getReference(T004Insurance.class, id);
                t004Insurance.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The t004Insurance with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
//            Collection<T005Patient> t005PatientCollectionOrphanCheck = t004Insurance.getT005PatientCollection();
//            for (T005Patient t005PatientCollectionOrphanCheckT005Patient : t005PatientCollectionOrphanCheck) {
//                if (illegalOrphanMessages == null) {
//                    illegalOrphanMessages = new ArrayList<String>();
//                }
//                illegalOrphanMessages.add("This T004Insurance (" + t004Insurance + ") cannot be destroyed since the T005Patient " + t005PatientCollectionOrphanCheckT005Patient + " in its t005PatientCollection field has a non-nullable t004Insurance field.");
//            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<T002Doctor> t002Doctors = t004Insurance.getT002Doctors();
            for (T002Doctor t002DoctorsT002Doctor : t002Doctors) {
                t002DoctorsT002Doctor.getT004Insurances().remove(t004Insurance);
                t002DoctorsT002Doctor = em.merge(t002DoctorsT002Doctor);
            }
            em.remove(t004Insurance);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<T004Insurance> findT004InsuranceEntities() {
        return findT004InsuranceEntities(true, -1, -1);
    }

    public List<T004Insurance> findT004InsuranceEntities(int maxResults, int firstResult) {
        return findT004InsuranceEntities(false, maxResults, firstResult);
    }

    private List<T004Insurance> findT004InsuranceEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(T004Insurance.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public T004Insurance findT004Insurance(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(T004Insurance.class, id);
        } finally {
            em.close();
        }
    }

    public int getT004InsuranceCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<T004Insurance> rt = cq.from(T004Insurance.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
